/**
 * 
 */
package br.financeiro.adapters;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import br.financeiro.R;
import br.financeiro.entities.Phone;
import br.financeiro.enums.TypePhone;
import br.financeiro.masks.EditMask;

/**
 * @author evaldosavio
 * 
 */
public class ItemPhoneFormAdapter extends BaseAdapter {
	private List<Phone> phones;
	private LayoutInflater inflater;
	private AlertDialog.Builder dialog;
	private DialogInterface.OnClickListener l;
	private final Context context;

	public ItemPhoneFormAdapter( Context context, List<Phone> phone ) {
		this.inflater = LayoutInflater.from( context );
		this.phones = phone;
		this.context = context;
	}

	private void createDialogTypes( final Phone phone ) {
		dialog = new AlertDialog.Builder( context );
		dialog.setTitle( "Selecione o tipo de telefone" );
		List<String> types = new ArrayList<String>();

		for ( TypePhone type : TypePhone.values() ) {
			types.add( type.toString() );
		}

		l = new DialogInterface.OnClickListener() {

			@Override
			public void onClick( DialogInterface dialog, int which ) {
				phone.setType( TypePhone.values()[which] );
				dialog.cancel();
				notifyDataSetChanged();
			}
		};

		dialog.setSingleChoiceItems( types.toArray( new String[types.size()] ), phone.getType().ordinal(), l );
	}

	@Override
	public int getCount() {
		return phones.size();
	}

	@Override
	public Object getItem( int id ) {
		return phones.get( id );
	}

	@Override
	public long getItemId( int id ) {
		return id;
	}

	@Override
	public View getView( int position, View view, ViewGroup groupView ) {
		if ( view == null ) {
			view = inflater.inflate( R.layout.form_phone, null );
		}
		// configure edit
		EditText edit = (EditText) view.findViewById( R.id.editPersonPhone );
		edit.setText( phones.get( position ).getNumber() );
		
		String mask = "(__)____-____";
		EditMask editMask = new EditMask( edit, mask );
		edit.addTextChangedListener( editMask );
		setEditFocusChange( position, edit, mask.length() );
		
		ImageButton btn = (ImageButton) view.findViewById( R.id.type );
		btn.setImageResource( phones.get( position ).getType().getIcon() );
		setButtonAction( position, btn );
		Button btnRemove = (Button) view.findViewById( R.id.btnRemovePhone );
		btnRemove.setTag( phones.get( position ));
		btnRemove.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick( View v ) {
				Phone p = (Phone) v.getTag();
				phones.remove( p );
				notifyDataSetChanged();
			}
		} );
		return view;
	}

	private void setButtonAction( int position, ImageButton btn ) {
		btn.setTag( phones.get( position ) );
		btn.setOnClickListener( new OnClickListener() {

			@Override
			public void onClick( View v ) {
				Phone phone = (Phone) v.getTag();
				createDialogTypes( phone );
				dialog.show();
			}
		} );
	}

	// configure button set type email
	private void setEditFocusChange( int position, final EditText edit, final int min ) {
		edit.setTag( phones.get( position ) );
		edit.setOnFocusChangeListener( new OnFocusChangeListener() {
			@Override
			public void onFocusChange( View v, boolean hasFocus ) {
				if ( !hasFocus ) {
					if ( min > edit.getText().toString().length() )
						edit.setText( "" );

					EditText edit = (EditText) v;
					Phone phone = (Phone) edit.getTag();
					phone.setNumber( edit.getText().toString() );
				}
			}
		} );
	}
}
